
package convenios;

import javax.swing.DefaultListModel;


public class TelaPrincipal extends javax.swing.JFrame {

   
    public TelaPrincipal(User user, Database dados) {
        localuser = user;
        database = dados;

        initComponents();

        //coloca nome nos campos
        textNomeUser.setText(localuser.getNome() + " (" + localuser.getSiape() + ")");

        //bloqueia botoes pros usuarios comuns
        if (localuser.getTipo() == 0) {
            botaoNovoConv.setEnabled(false);
        }

       //carrega o nome dos convenios na tela inicial
      loadConv();

        

    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botaoNovoConv = new javax.swing.JButton();
        botaoBusca = new javax.swing.JButton();
        campoNumConv = new javax.swing.JTextField();
        comboTipoBusca = new javax.swing.JComboBox();
        botaoRelat = new javax.swing.JButton();
        textoTop = new javax.swing.JLabel();
        comboTempoConv = new javax.swing.JComboBox();
        textNomeUser = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        textoMid = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        botaoOpcoes = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema de Controle de Convenios");
        setResizable(false);

        botaoNovoConv.setText("Novo");
        botaoNovoConv.setToolTipText("Cadastrar novo Convenio.");
        botaoNovoConv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoNovoConvActionPerformed(evt);
            }
        });

        botaoBusca.setText("Buscar");
        botaoBusca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoBuscaActionPerformed(evt);
            }
        });

        comboTipoBusca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nº Convenio", "Nº Processo" }));

        botaoRelat.setText("Tela de Relatórios");

        textoTop.setText("Visualizar convênios perto do fim da vigência:");

        comboTempoConv.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Menos de 1 mês", "Menos de 2 meses", "Menos de 3 meses" }));
        comboTempoConv.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboTempoConvActionPerformed(evt);
            }
        });

        textNomeUser.setText("nulo nulo");

        textoMid.setText("Acompanhar Convenio:");

        botaoOpcoes.setText("Opções");
        botaoOpcoes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoOpcoesActionPerformed(evt);
            }
        });

        jList1.setModel(modelconv);
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(textNomeUser)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 234, Short.MAX_VALUE)
                                .addComponent(botaoOpcoes))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(campoNumConv, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(comboTipoBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botaoBusca)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botaoNovoConv))
                            .addComponent(textoMid)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(textoTop)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(comboTempoConv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botaoRelat)
                        .addGap(120, 120, 120))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textNomeUser)
                    .addComponent(botaoOpcoes, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textoTop)
                    .addComponent(comboTempoConv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textoMid)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoNumConv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboTipoBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botaoBusca)
                    .addComponent(botaoNovoConv))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botaoRelat, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(65, 65, 65))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboTempoConvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboTempoConvActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboTempoConvActionPerformed

    private void botaoNovoConvActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoNovoConvActionPerformed
        //
        //evento para o botão de criar novo convenio
        //
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaAddConv(database).setVisible(true);
            }
        });
    }//GEN-LAST:event_botaoNovoConvActionPerformed

    private void botaoOpcoesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoOpcoesActionPerformed
        //
        //evento para o botão de opcoes
        //
        
        TelaOpcoes tela = new TelaOpcoes(localuser,database);
        tela.setVisible(true);




    }//GEN-LAST:event_botaoOpcoesActionPerformed

    private void botaoBuscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoBuscaActionPerformed
        //
        //evento para o botão de busca
        //
         
        Convenio conv = database.getConveniobyID(Integer.parseInt(campoNumConv.getText()));

        if (conv != null) {
            //Inicia janela
            TelaAcomp tela = new TelaAcomp(database,conv);
            tela.setVisible(true);
        
        }


    }//GEN-LAST:event_botaoBuscaActionPerformed
  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoBusca;
    private javax.swing.JButton botaoNovoConv;
    private javax.swing.JButton botaoOpcoes;
    private javax.swing.JButton botaoRelat;
    private javax.swing.JTextField campoNumConv;
    private javax.swing.JComboBox comboTempoConv;
    private javax.swing.JComboBox comboTipoBusca;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JLabel textNomeUser;
    private javax.swing.JLabel textoMid;
    private javax.swing.JLabel textoTop;
    // End of variables declaration//GEN-END:variables
      private Database database = new Database();
      private User localuser;
     private DefaultListModel modelconv = new DefaultListModel();


    private void loadConv() {
        //
        //metodo para carregar na tela o resumo dos convenios de cada usuario
        //

        String listconv[] = database.getConvbyUser(localuser);

        for (int i = 0; i < listconv.length; i++) {
                 modelconv.addElement(listconv[i]);
        }

        
    }

}
